package leetcode.每日一题;

import org.junit.Test;

import java.util.ArrayList;

/**
 * @author ：zsy
 * @date ：Created 2022/10/28 2:32 PM
 * @description：
 */
public class 子数组的最小值之和 {
    @Test
    public void test() {
        System.out.println(new Solution().sumSubarrayMins(new int[]{3, 1, 2, 4}));
    }

    class Solution {

        int ans = 0;

        public int sumSubarrayMins(int[] arr) {
            dfs(arr, new ArrayList<>(), Integer.MAX_VALUE, 0);
            return ans;
        }

        private void dfs(int[] arr, ArrayList<Integer> list, int val, int idx) {
            if (idx == arr.length) {
                System.out.print(list);
                System.out.println("----->" + val);
                ans += val == Integer.MAX_VALUE ? 0 : val;
                return;
            }
            // 拿
            list.add(arr[idx]);
            int min = val;
            if (arr[idx] < val) {
                min = arr[idx];
            }
            dfs(arr, list, min, idx + 1);
            // 不拿
            list.remove(list.size() - 1);
            if (list.size() == 0 || list.get(list.size() - 1) == arr[idx - 1] || idx == arr.length - 1) {
                dfs(arr, list, val, idx + 1);
            }
        }
    }
}
